import logging

import util.dtest
import util.common
import test.neon_support as ns

print '''
This is an example test. It creates two nodes and runs the tx daemon
on each. After 1 second, it kills both daemons
'''

dtest = util.dtest.DTest()
cond = ns.Conditions(dtest)

node_foo = dtest.add_node(ns.NeonNode('foo'))
node_bar = dtest.add_node(ns.NeonNode('bar'))
dtest.set_mapper(util.dtest.LocalMapper())

dtest.configure()
dtest.setup_files()

def test_script(test_runner):
    util.common.sleep_sec(1)

    # Note: We need to use exec_f to wrap any functions that touch
    # state that could be shared with DTest because of multithreading.
    node_foo.run_tx()
    util.common.sleep_sec(1)
    node_bar.run_tx()

    test_runner.wait_for(10, cond.on_all(cond.tx_is_running))
    util.common.sleep_sec(1)
    
    test_runner.exec_f(node_foo.tx_proc.kill)
    test_runner.exec_f(node_bar.tx_proc.kill)

    test_runner.wait_for(10, cond.on_all(cond.tx_is_not_running))
    
dtest.run(test_script)
